import json
import random
import time

import pymysql
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

# 连接MySQL数据库
try:
    connection = pymysql.connect(
        host='localhost',
        user='root',
        password='zhang77is',
        db='test',
        charset='utf8mb4',
        cursorclass=pymysql.cursors.DictCursor
    )
    if connection:
        print("Connected to the database")
        cursor = connection.cursor()

        connection.commit()

except Exception as e:
    print(f'Error while connecting: {e}')

finally:
    if connection:
        connection.close()
        print("Connection closed")


def get_news_info(page=1):
    # 设置 ChromeDriver 路径
    chrome_driver_path = "/usr/local/bin/chromedriver"
    # 配置 Chrome 选项
    options = Options()
    options.add_argument("--disable-blink-features=AutomationControlled")
    options.add_argument("--headless")  # 启用无头模式
    options.add_argument("--disable-gpu")  # 禁用 GPU 加速（某些情况下需要）
    driver = webdriver.Chrome(service=Service(executable_path=chrome_driver_path), options=options)

    driver.get("http://www.ahsczx.com/Welcome/NewsList.aspx?NewsClassID=17&FaterID=16&page=" + str(page))

    wait = WebDriverWait(driver, 20)
    # 查找DataListAll标签
    data_list_all = WebDriverWait(driver, 10).until(
        EC.presence_of_element_located((By.ID, 'DataListAll'))
    )

    # 查找表格中的所有<li>标签
    li_tags = data_list_all.find_elements(By.TAG_NAME, 'li')

    for index_, li in enumerate(li_tags):
        a = li.find_element(By.TAG_NAME, 'a')
        href = a.get_attribute('href')
        print('\n')
        driver.execute_script("window.open('');")
        driver.switch_to.window(driver.window_handles[-1])
        # 访问超链接
        driver.get(href)
        # 等待页面加载完成
        wait = WebDriverWait(driver, 10)
        title = wait.until(EC.presence_of_element_located((By.CLASS_NAME, 'shownewstitle'))).text
        author = wait.until(EC.presence_of_element_located((By.ID, 'lbzuozhe'))).text
        source = wait.until(EC.presence_of_element_located((By.ID, 'lblaiyuan'))).text
        publish_time = wait.until(EC.presence_of_element_located((By.ID, 'lbtime'))).text
        view_times = wait.until(EC.presence_of_element_located((By.ID, 'lbdianjishu'))).text
        content = wait.until(EC.presence_of_element_located((By.CLASS_NAME, 'shownewscontent'))).text

        # 获取 shownewscontent 类下的所有 img 标签的 src 属性
        img_elements = driver.find_elements(By.CSS_SELECTOR, '.shownewscontent img')
        img_src = [img.get_attribute('src') for img in img_elements]
        img_srcs_string = json.dumps(img_src)

        datas = {
            'title': title,
            'author': author,
            'source': source,
            'publish_time': publish_time,
            'view_times': view_times,
            'content': content,
            'img_src': img_srcs_string
        }
        print(datas)

        # 删除已存在的记录
        delete_query = """
        DELETE FROM article WHERE title = %s;
        """
        delete_data = (title,)
        cursor.execute(delete_query, delete_data)

        # 插入数据到MySQL
        insert_query = """
        INSERT INTO article (title,create_time, author, source, publish_time,
         view_times,content,img_src)
        VALUES ( %s,now(), %s,%s, %s,
        %s,%s,%s);
        """
        data = (title, author, source, publish_time, view_times, content, img_srcs_string)
        cursor.execute(insert_query, data)
        connection.commit()

        if index_ == 5:
            sleep_time = random.uniform(1, 5)  # 随机休眠1到5秒
            print(f"Sleeping for {sleep_time:.2f} seconds...")
            time.sleep(sleep_time)

        # 关闭当前标签页
        driver.close()
        driver.switch_to.window(driver.window_handles[0])

    driver.quit()


if __name__ == '__main__':
    # 连接MySQL数据库
    connection = pymysql.connect(
        host='localhost',
        user='root',
        password='zhang77is',
        db='test',
        charset='utf8mb4',
        cursorclass=pymysql.cursors.DictCursor
    )
    cursor = connection.cursor()

    for i in range(3):
        get_news_info(i)

    # 关闭数据库连接
    if connection:
        cursor.close()
        connection.close()
        print("MySQL connection is closed")
